#include "MatrixKey.h"
#include "LCD1602.h"

unsigned int Password=0;
unsigned int ans=1111;           //正确密码
unsigned char cnt,Key;

void main()
{
	LCD_Init();
	LCD_ShowString(1,1,"Password:");
	LCD_ShowNum(2,1,0,4);        //四位密码
	while(1)
	{
		Key=ButtonDown();
		if(Key<=10 && Key !=0 && cnt<4)              //0~9输入密码
		{
			Key%=10;
			Password=10*Password+Key;
			LCD_ShowNum(2,1,Password,4);
			cnt++;               //计次4位
		}
		if(Key == 11)            //S11为确认键
		{
			if(Password == ans)  //密码正确
			{
				LCD_ShowString(1,14,"OK ");
				cnt=0;           //计数清零
				Password=0;      //密码清零
			}
			else                 //密码错误
			{
				LCD_ShowString(1,14,"ERR");
				cnt=0;           //计数清零
				Password=0;      //密码清零
			}
			LCD_ShowNum(2,1,Password,4);
		}
		if(Key == 12)            //S12为删除键
		{
			Password/=10;
			LCD_ShowNum(2,1,Password,4);
			cnt--;
		}
	}
	
	return;
}